"""
@author: xxw
@file: 01.正则表达式.py
@time: 2023/5/30 9:47
"""
# 正则表达式
# 1、文本过滤
# 2、合法性校验

import re

############查找匹配
# search match
# search 和 match 都是查找第一个匹配
# match是从头部字符开始查找，只匹配字符串的开始
# search 可以在任意位置匹配

# 使用r标志位  输入原始字符串

# findall finditer
# 查找所有匹配项
# findall --》返回一个列表
# finditer --》返回一个可迭代对象返回

# 正则替换
# re.sub("匹配正则","替换内容",”原始字符串“)
# print(re.sub(r"[A-Z]","*","SFSAD234"))

# 正则编译
# reg = re.compile(r"[A-Z]")
# print(reg.findall("A5bUsasf"))

# ########基本正则
# 区间匹配 [] 可以按照编码顺序规定范围
# 在区间范围内，匹配任意一个
# ret = re.findall("[pPmM]","pythonPythonmath")
# print(ret)
# ret = re.findall(r"[0-9]","24.234324fasd")
# print(ret)
# ret = re.findall(r"[A-z]","24.234324fasd")
# print(ret)
# ret = re.findall(r"[A-Za-z0-9][0-9]", "24.234324fasd")
# print(ret)
# ret = re.findall("[^A-Z]c", "acBcc2Ccd")

# 匹配或  |
# msg = "welcome to changsha, welcome to hunan"
# ret = re.findall("changsha|hunan", msg)
# print(ret)

# . 占位符 表示除了\n以外的任意一个字符
# ret = re.findall("p.thon","pythondsafsakdfpasfdk thon p \nthon")
# print(ret)

# 匹配开始和结束
# ^ $

# number = input("请输入您的电话号码")
# if re.findall('^1\d{10}$',number):
#     print("电话号码合法")
# else:
#     print("电话号码不合法!")
# while 1:
#     number = input("请输入一个数(exit退出):")
#     if re.findall(r'^\+?[1-9]\d*$', number):
#         print("是正整数")
#     elif re.findall(r'^-[1-9]\d*$', number):
#         print("是负整数")
#     elif re.findall(r"^[-+]?\d+\.\d+", number):
#         print("小数")
#     elif number == "exit":
#         break
#     else:
#         print("其他")
# number = input("请输入一个数:")
# if re.findall(r'^[1-9]\d?$|^1\d{1,2}$|^2[0-4]\d$|^25[0-5]$', number):
#     print("是1-255内")
# else:
#     print("不是")

# 贪婪匹配和非贪婪匹配
# 贪婪模式，尽可能匹配长的字符串
# 非贪婪模式，匹配到就输出 +? *?
# msg = "helloooooooooo"
# print(re.findall("lo{3,}",msg))
# msg2 = "cats and dogs, cats1 and dog1"
# print(re.findall("cats.*?s", msg2))

# 正则分组
# 使用括号结合正则表达式
# ret = re.search(r"(\d[a-z]){3}")
# print(ret)
# ret = re.search(r"(\d{3}[a-z])-(\d{4})","343t-8888sd")
# print(ret)
# print(ret.group(0))
# print(ret.group(1))
# print(ret.group(2))

# 捕获分组
#   分组之后匹配到的数据都是暂时会放在内存里，并且给定一个从1开始的索引
#   捕获分组可以向后引用

# msg = "<div><a href='http://baidu.com'>链接百度</div>"
# print(re.search(r"<(\w+)>.*</\1>", msg))
# print(re.search(r"(\d[a-z])(\d[a-z]) \1 \2", "2a 3d 2a 3d"))

# 非捕获分组
# (?: regex)
# 只分组不捕获，不会将匹配到的内容临时存放在内存里，不会分配索引，不能使用分组向后引用
# ret = re.search(r"(?:\d{3})-(\d{4})-\1", "123-2222-2222")
# print(ret)

# 使用findall去匹配的时候，如果有捕获分组，只会显示捕获分组的内容
# ret = re.findall(r"(?:\d{3})-(\d{4})-\1", "123-2222-2222")
# print(ret)

# 从键盘接受身份证的输入
# 判断是否为合法的身份证 以4开头的18位数字，截取出出生年月日
# number = input("请输入身份证:")
# ret = re.findall(r"^4\d{5}(\d{8})\d{4}$", number)
# if ret:
#     print(ret[0])
# else:
#     print("身份证不合法")

# 正则标记
# re.I  大小写不敏感
# re.M  多行匹配
# re.S  使用 . 表示换行符在内的任意字符

# print(re.findall(r"python", "PYTHOn python", re.I))
# msg = '''
# python
# PYThon
# '''
# print(re.findall(r"^python$", msg, re.I|re.M))
# print(re.findall(r".+"), msg, re.S)

# 正则断言 -- 零宽断言
# 不占用匹配宽度，只是确定位置
# msg = "hellos world, hello sc"
# print(re.findall(r"o(?!s)", msg))
# print(re.findall(r"(?<=l)o", msg))
# print(re.findall(r"(?<!l)o", msg))
# msg = '''
# 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
#     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
#     inet 127.0.0.1/8 scope host lo
#        valid_lft forever preferred_lft forever
#     inet6 ::1/128 scope host
#        valid_lft forever preferred_lft forever
# 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
#     link/ether 00:0c:29:1e:73:3e brd ff:ff:ff:ff:ff:ff
#     inet 192.168.1.54/24 brd 192.168.1.255 scope global noprefixroute ens33
#        valid_lft forever preferred_lft forever
#     inet6 fe80::20c:29ff:fe1e:733e/64 scope link
#        valid_lft forever preferred_lft forever
# '''
# print(re.findall(r"(?<=inet ).*(?=/2)", msg))

# ip = input("请输入ip地址:")
# res = re.findall(r"^([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\. \2 \. \2$", ip)
# if res:
#     print("该ip地址合法")
# else:
#     print("该ip地址不合法")

# msg = "hello,world . 'welcome to changsha' test"
# print(re.findall(r"(?<=').*(?=')|\w+", msg))

with open("a.txt") as fp:
    tmp_dict = {}
    for line in fp:
        l = line.replace("\n", "")
        if re.findall(r"\b[0-9]\b", l):
            tmp_dict[l] = tmp_dict.get(l, 0) + 1

print(tmp_dict)
result = sorted(tmp_dict.items(), key=lambda x:x[1])




